<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/23
 * Time: 14:54
 */
class Domain_Index{
    public function getByUser(){  //测试
        $model=new Model_Index();
        return $model->getByUser();
    }
    /*
     * 发送用户信息 手机入库
     */
    public function getByPhone($obj){
        $model=new Model_Index();
        $info=$model->isPhone($obj);

        if(empty($info)){
            $model->getByPhone($obj);
        }
    }
    /*
     * 活动商家列表
     */
    public function getClientList(){
        $Model_Client = new Model_Client();
          //$Model_Client -> updateAllinfo();  //假数据
            return $Model_Client -> getClientList();

    }
    /*
     *假数据接口
     */
    public function falseInfo(){
        $Model_Client = new Model_Client();
        //$time11 = date('Y-m-dH:i:s',time());
        $t=strtotime('10:00:00');
        $t1=strtotime('12:00:00');
        $t2=strtotime('17:00:00');
        $t3=strtotime('21:00:00');

        $time1 = date('H:i:s',time());
        $time = strtotime($time1);
        $Model_Client -> updateAllinfo();

        if($time == $t){  // *1.2
            return  $Model_Client -> getFalseVote2();
        }elseif($time == $t1){  // *1.2
            return  $Model_Client -> getFalseVote2();
        }elseif($time == $t2){    // *1.2
            return  $Model_Client -> getFalseVote2();
        }elseif($time == $t3){   // *1.3
            return  $Model_Client -> getFalseVote3();
        }else{
            return $Model_Client -> getClientList();
        }

    }
    /*
     * 点击投票按钮，当前用户是否投满三家，投票权限
     */
    public function getCurrentPhone($obj){
        $Model_Current = new Model_Index();
        $info = $Model_Current -> getCurrentPhone($obj);
        $info1 = $Model_Current -> getCurrentPhone1($obj);
       // print_r($info);exit;
        if($info){  //有数据则没有投票权限
            return $info;
        }else{   //有投票权限
            return $info1;
        }
    }
    /**
     * 分享
     */
    public function doShare($obj){
        $model=new Model_Index();
        $info=$model->isShared($obj);
        if(!$info['share']){
            $model->doShare($obj);
        }
    }
    /**
     * 保存保单
     */
    public function addInsurance($obj){
        $model=new Model_Insurance();

        $info=$model->isInsuranceExists($obj);
        if(!$info){
            $res = $model->addInsurance($obj);
            if($res){
                return 0;
            }else{
                return 1;
            }
        }
        return 0;
    }
    /*
     * 投票   用户投票减1，抽奖机会加1  商家票数加1
     */
    public function setVote($obj){
        $Model_Vote = new Model_Index();
        $Model_Client = new Model_Client();
        $info1 =  $Model_Vote->setVote($obj);
        if($info1 == 1){
            if( $Model_Client->clientVote($obj)){
                return 0; //投票成功
            }else{
                return 1;
            }
        }else{
            return 1;
        }
    }
    /*
     * 用户当天抽奖
     */
    public function getGift($obj){
        $_SESSION['phone'] = $obj->phone;

        $Model_Gift = new Model_Index();
        $Model_Prize = new Model_Lottery();
        $prizecount = $Model_Prize->giftCount();
        //$usergift = $Model_Gift ->getGift($obj);
        //判断用户是否有中奖机会
       $chance =  $Model_Gift -> isChance($obj);

        if(count($chance)==0){
            return -3;
        }
        if($chance[0]['lottery'] == 0){
           return -3;  //没有抽奖机会
        }


        if($prizecount == 0){   //如果奖池为空   用户抽奖机会减 1
            $Model_Gift -> decLottery($obj);       //中奖了，用户抽奖机会减 1
            return -2;
        }else{
            $model = new Common_Function();
            //给奖品
            $prize = $Model_Prize -> getPrize($obj);
            if($prize){
                $Model_Gift -> writePrize($obj);  //中了奖,写入库中
                $Model_Gift -> decLottery($obj);       //中奖了，用户抽奖机会减 1
            }
            //推送奖品
            //echo $prize;
            if($prize == 1) {
                //推送1元优惠券
                $model ->setFanJuan('7C7523');
                return 1;
            }elseif($prize == 3){
                //发送3元优惠券
                $model ->setFanJuan('49E185');
                return 3;
            }elseif($prize == 5){
                //发送5元优惠券
                $model ->setFanJuan('2854D8');
                return 5;
            }elseif($prize == 6){

                return 6;
            }

        }

    }
}